using System;
using System.Collections.Generic;
using System.IO;
using Common;

namespace SProject.Program
{
	[Serializable]
	public class Segment : BaseSegment
	{
		private List<Step> _Items = new List<Step>();

		public string Name { get; set; }

		public int Cycles { get; set; }

		public List<Step> Items
		{
			get
			{
				return _Items;
			}
		}

		public Segment()
		{
			Cycles = 1;
		}

		public override void Copy(BaseSegment BaseSegment)
		{
			Segment segment = BaseSegment as Segment;
			if (segment != null)
			{
				Name = segment.Name;
				Cycles = segment.Cycles;
				_Items.Clear();
				for (int i = 0; i < segment.Items.Count; i++)
				{
					_Items.Add(segment.Items[i].Clone());
				}
			}
		}

		public override BaseSegment Clone()
		{
			Segment segment = new Segment();
			segment.Copy(this);
			return segment;
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null)
			{
				return false;
			}
			Segment segment = obj as Segment;
			if (segment == null)
			{
				return false;
			}
			if (base.Equals(segment))
			{
				return true;
			}
			if (Cycles != segment.Cycles || Items.Count != segment.Items.Count)
			{
				return false;
			}
			for (int i = 0; i < Items.Count; i++)
			{
				if (!Items[i].Equals(segment.Items[i]))
				{
					return false;
				}
			}
			return true;
		}

		public override void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_Segment);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			if (!string.IsNullOrEmpty(Name))
			{
				list.Add(PropertyKey.Key_Name);
				list2.Add(BytesConverter.GetBytes(Name));
			}
			list.Add(PropertyKey.Key_Cycles);
			list2.Add(BytesConverter.GetBytes(Cycles));
			list.Add(PropertyKey.Key_Items);
			StreamEx streamEx = new StreamEx();
			streamEx.Write(BytesConverter.GetBytes(Items.Count));
			foreach (Step item in Items)
			{
				item.SaveToStream(streamEx);
			}
			streamEx.Seek(0L, SeekOrigin.Begin);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public override bool ReadFromStream(StreamEx stream)
		{
			Items.Clear();
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_Segment)
			{
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_Index)
				{
					List<PropertyKey> keys;
					List<byte[]> dates;
					stream.ReadIndex(out keys, out dates);
					for (int i = 0; i < keys.Count; i++)
					{
						switch (keys[i])
						{
						case PropertyKey.Key_Name:
							Name = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_Cycles:
							Cycles = BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_Items:
						{
							StreamEx streamEx = new StreamEx();
							streamEx.Write(dates[i]);
							streamEx.Seek(0L, SeekOrigin.Begin);
							int num = streamEx.ReadInt();
							for (int j = 0; j < num; j++)
							{
								Step step = new Step();
								step.ReadFromStream(streamEx);
								Items.Add(step);
							}
							streamEx.Close();
							break;
						}
						}
					}
					return true;
				}
				stream.Seek(-4L, SeekOrigin.Current);
				while (true)
				{
					switch (stream.ReadKey())
					{
					case PropertyKey.Key_Name:
						Name = stream.ReadString();
						break;
					case PropertyKey.Key_Cycles:
						Cycles = stream.ReadInt();
						break;
					case PropertyKey.Key_Count:
					{
						int num2 = stream.ReadInt();
						for (int k = 0; k < num2; k++)
						{
							Step step2 = new Step();
							step2.ReadFromStream(stream);
							Items.Add(step2);
						}
						break;
					}
					default:
						stream.Seek(-4L, SeekOrigin.Current);
						return true;
					}
				}
			}
			stream.Seek(-4L, SeekOrigin.Current);
			return false;
		}
	}
}
